package com.boarsoft.boar.batch.demo.flow.bigfile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.boarsoft.flow.core.bean.ForkHandler;

/**
 * @ClassName:  LoadDpfm01Fork   
 * @Description:ForkHandler决定后面fork的线程数，以及给后面fork的线程传递数据
 * @author: 
 * @date:   2019年2月15日 上午9:50:59   
 * @Copyright: 2019
 */
public class LoadDpfm01Fork implements ForkHandler {
	private static final Logger log = LoggerFactory.getLogger(LoadDpfm01Fork.class);
	
	public static long loadDpfm01ForkStart;
	
	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> fork(String key, Object data, Throwable throwable) {
		
		loadDpfm01ForkStart = System.currentTimeMillis();
		
		Map<String, Object> fromMap = (Map<String, Object>) data;
		List<String> fileList = new ArrayList<>();
		fileList.addAll((List<String>)fromMap.get("list"));
		int fileCount = fileList.size();

//		log.info("ForkHandler接收map: " + fromMap);
//		System.out.println("文件数：" + fileCount);
//		System.out.println("文件列表：" + fileList);
		
		Map<String, Object> returnMap = new HashMap<String, Object>();
		for (int p = 0; p < fileCount; p++) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.putAll(fromMap);
			map.put("fileName", fileList.get(p));
			returnMap.put("p".concat(String.valueOf(p)), map);
		}
//		for (String str : returnMap.keySet()) {
//			Map<String, Object> valueMap = (Map<String, Object>) returnMap.get(str);
//			log.info("ForkHandler返回key: " + str + "  value: " + valueMap);
//		}
		
		System.out.println("开始分线程加载dpfm01数据...");
		return returnMap;
	}
}
